package com.yvon.maple.service.seckill.entity;

import cn.hutool.core.date.DatePattern;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yvon.boot.mybatis.model.BaseEntity;
import com.yvon.maple.enums.SeckillActivityStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 秒杀活动
 *
 * @author Yvon
 * @date 2024-10-24
 */
@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@TableName("seckill_activity")
public class SeckillActivity extends BaseEntity implements Serializable {

    /**
     * 活动名称
     */
    private String activityName;

    /**
     * 活动开始时间
     */
    @DateTimeFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
    @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
    private LocalDateTime startTime;

    /**
     * 活动结束时间
     */
    @DateTimeFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
    @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
    private LocalDateTime endTime;

    /**
     * 活动状态 0：已发布； 1：上线； -1：下线
     */
    private Integer status;

    /**
     * 活动描述
     */
    private String activityDesc;

    public boolean validateParams() {
        if (StringUtils.isEmpty(activityDesc)
                || startTime == null
                || endTime == null
                || endTime.isBefore(startTime)
                || endTime.isBefore(LocalDateTime.now())) {
            return false;
        }
        return true;
    }

    public boolean isOnline() {
        return SeckillActivityStatus.isOnline(status);
    }

    public boolean isInSeckilling() {
        LocalDateTime currentDate = LocalDateTime.now();
        return startTime.isBefore(currentDate) && endTime.isAfter(currentDate);
    }
}
